package com.myspring.web.view.excel;

import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractCachingViewResolver;

import java.util.Locale;

/**
 * 实现order接口，有优先级
 * 参考XmlViewResolver
 * @author Administrator
 *
 */
public class ExcelViewResolver extends AbstractCachingViewResolver implements Ordered {
	private ApplicationContext context;
	
	private int order = Integer.MAX_VALUE;  // default: same as non-Ordered
	
	public void setOrder(int order) {
		this.order = order;
	}

	@Override
	public int getOrder() {
		return this.order;
	}
	
	@Override
	public boolean isCache() {
		return false;
	}

	/**
	 * 直接在容器中配置视图，单例获取bean，不用每次新建，待优化 TODO
	 */
	@Override
	protected View loadView(String viewName, Locale locale) throws Exception {
		context = super.getApplicationContext();

//		参考配置文件，如果配置了优先级，就会有限使用此视图解析器，就会抛出NoSuchBeanDefinitionException TODO
		if (context != null) {
			return context.getBean(viewName, View.class);
		}
		return null;
	}

}
